<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\goods\Category as GoodsCategory;
use app\common\model\goods\category\Region as CategoryRegion;
use app\common\model\goods\Goods as GoodsGoods;
use app\common\model\goods\Region;
use app\common\model\user\Info;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * @Author LK
     * @ApiTitle (商品分类（普通商品）)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category()
    {
        $lists = GoodsCategory::all(['pid' => 0], ['child']);
        $this->success('请求成功', $lists);
    }

    /**
     * @Author LK
     * @ApiTitle (商品列表)
     * @ApiMethod (GET)
     * @ApiParams (name="first_cate", type="integer", required=true, sample="", description="一级分类")
     * @ApiParams (name="two_cate", type="integer", required=true, sample="", description="二级分类")
     * @ApiParams (name="keywords", type="string", required=true, sample="", description="关键字")
     * @ApiParams (name="sales", type="string", required=true, sample="", description="降序：desc；升序：asc")
     * @ApiParams (name="min_fee", type="string", required=true, sample="", description="降序：desc；升序：asc")
     * @ApiParams (name="type", type="string", required=true, sample="", description="1=商品区域,2=体验区域,3=积分区域,4=vip区域")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $params = request()->param();
        extract($params);
        $model = new GoodsGoods;
        if (isset($first_cate) && $first_cate) {
            $model->where('first_cate', $first_cate);
        }
        if (isset($two_cate) && $two_cate) {
            $model->where('two_cate', $two_cate);
        }
        if (isset($keywords) && $keywords) {
            $model->whereLike('title', '%' . $keywords . '%');
        }
        if (isset($sales) && $sales) {
            $sort .= 'sales ' . $sales;
        } else if (isset($min_fee) && $min_fee) {
            $sort .= 'min_fee ' . $min_fee;
        } else {
            $sort = 'weigh desc';
        }
        $model->where('status', 'normal');
        $model->where('type', $type);
        $lists = $model->with(['suk'])->order($sort)->paginate(10);
        $this->success('请求成功', $lists);
    }

    /**
     * @Author LK
     * @ApiTitle (商品详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="商品id")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $params = request()->param();
        extract($params);
        $goods = GoodsGoods::get($id, ['suk']);
        $this->success('请求成功', $goods);
    }
}
